import sys
from PyQt5.QtCore import QThread, pyqtSignal

from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QPropertyAnimation

from com.atzl.interaction.WindowUpDateDataBase import UpDateWindow
from com.atzl.interaction.WindowPassWord import PassWordWindow
from com.atzl.interaction.WindowMessage import MessageWindow
from com.atzl.common.Win import Windows
from com.atzl.common.Datas import MessagesData
from com.atzl.common.StatusCode import StatusCode
from com.atzl.common.ActionStates import ActionStates
from com.atzl.functions.accounting.func.UpdatePrice import UpdatePrice
from com.atzl.functions.accounting.func.Logs import CheckLogs
from com.atzl.ui.SetOfferUI import Ui_MainWindow as setoffer
from com.atzl.functions.accounting.func.SetPrice import SetPrice
from com.atzl.common.States import States
from com.atzl.interaction.WindowPriceTable import PriceTableWindow
from com.atzl.interaction.WindowNwPriceTable import NWPriceTableWindow
from com.atzl.functions.accounting.func.SimulstionOperation import SimulationOperation


# 设置报价窗口交互
class SetOfferWindow(QMainWindow, UpdatePrice):  # 继承:QMainWindow, CheckLogs

    def __init__(self):
        QMainWindow.__init__(self)
        # 设置为全局小部件
        self.ui = setoffer()
        self.set = SetPrice()
        self.so = SimulationOperation()
        self.password = PassWordWindow()
        self.message = MessageWindow()
        self.update = UpDateWindow()
        self.setprice = PriceTableWindow()
        self.nwsetprice = NWPriceTableWindow()

        self.m_flag = False
        self.ui.setupUi(self)
        global widgets
        widgets = self.ui
        global set
        set = self.set
        global so
        so = self.so


        # 创建报价子页面
        widgets.pushButton_9.clicked.connect(self.close_app)  # 关闭应用程序
        widgets.pushButton_10.clicked.connect(self.showMinimized)  # 窗口最小化
        widgets.pushButton_11.clicked.connect(self.close_app)  # 关闭应用程序
        widgets.pushButton_12.clicked.connect(self.showMinimized)  # 窗口最小化
        widgets.pushButton_13.clicked.connect(self.showMinimized)  # 窗口最小化
        widgets.pushButton_14.clicked.connect(self.close_app)  # 关闭应用程序
        widgets.pushButton_4.clicked.connect(widgets.lineEdit.clear)  # 清空文本输入框
        widgets.pushButton_4.clicked.connect(widgets.lineEdit_3.clear)  # 清空文本输入框
        widgets.pushButton_4.clicked.connect(widgets.lineEdit_5.clear)  # 清空文本输入框
        widgets.pushButton_4.clicked.connect(widgets.textBrowser.clear)  # 清空浏览框
        widgets.pushButton_4.clicked.connect(widgets.textBrowser_2.clear)  # 清空浏览框
        widgets.pushButton_4.clicked.connect(widgets.textBrowser_3.clear)  # 清空浏览框
        widgets.pushButton_4.clicked.connect(widgets.textBrowser_4.clear)  # 清空浏览框
        widgets.pushButton_2.clicked.connect(widgets.textEdit.clear)  # 清空文本输入框
        widgets.pushButton_3.clicked.connect(self.set_price_by_excel)  # 触发导入报价按钮
        widgets.pushButton_15.clicked.connect(self.rollback_navigation)  # 返回导航页面
        widgets.pushButton_16.clicked.connect(self.rollback_navigation)  # 返回导航页面
        widgets.pushButton_17.clicked.connect(self.rollback_navigation)  # 返回导航页面
        widgets.pushButton_20.clicked.connect(set.look_photo)  # 查看图片
        widgets.pushButton.clicked.connect(self.response_result)  # 模拟运算
        widgets.pushButton_21.clicked.connect(self.goto_updatewin)  # 显示修改报价页面

        self.completer = QCompleter(States.STATES)
        # 设置匹配模式  有三种： Qt.MatchStartsWith 开头匹配（默认）  Qt.MatchContains 内容匹配  Qt.MatchEndsWith 结尾匹配
        self.completer.setFilterMode(QtCore.Qt.MatchContains)
        # 设置补全模式  有三种： QCompleter.PopupCompletion（默认）  QCompleter.InlineCompletion   QCompleter.UnfilteredPopupCompletion
        self.completer.setCompletionMode(QCompleter.PopupCompletion)
        widgets.lineEdit_3.setCompleter(self.completer)


    # 查看预览模板
    def look_the_photo(self):
        self.logs_in(ActionStates.OPENMODEPHOTO)  # 回传用户行为
        set.look_photo()  # 打开图片


    # 创建报价子页面
    def analysis_path(self):
        MessagesData.ANALYSIS_PATH_RICE = widgets.textEdit.toPlainText().replace('file:///', '')  # 解析文件路径

    #
    def set_price_by_excel(self):
        self.analysis_path()
        z = MessagesData.ANALYSIS_PATH_RICE
        if z == '' or z is None:
            self.iftext_null()
            return
        widgets.textEdit.clear()
        self.show_table()
        self.logs_in(ActionStates.CREATEBILLTOPASSWORD)  # 将日志


    def show_table(self):
        datax = set.get_field_message(MessagesData.ANALYSIS_PATH_RICE)
        if str(datax) == str(StatusCode.TABLE_ISHAVE):
            self.ifhave_table()
        elif str(datax) == str(StatusCode.OPERATION_LOSE):
            self.to_message(StatusCode.OPERATION_LOSE)
            self.logs_in(StatusCode.OPERATION_LOSE)
        elif set.if_nw_table(MessagesData.OBJ_ID_PRICE) == 0:
            nw_data = set.get_nw_price(MessagesData.OBJ_ID_PRICE)
            self.to_pricetable(datax)
            self.to_nw_pricetable(nw_data)
        else:
            self.to_pricetable(datax)

    # 如果有报价表界面提示并返回原来界面和回传用户行为监控数据至数据库
    def ifhave_table(self):
        self.to_message("该客户报价已创建!")  # 消息提示
        self.logs_in(ActionStates.HINTREPETITIONSETOFFER)  # 回传用户行为


    def to_pricetable(self, data):
        MessagesData.SET_PRICE = data
        Windows.PRICETABLE_WIN = self.setprice
        Windows.PRICETABLE_WIN.set_table_data()
        Windows.PRICETABLE_WIN.show()
        self.logs_in(ActionStates.RIGHTPASSWORDCUTOFFERTABLE)

    def to_nw_pricetable(self, data):
        MessagesData.SET_NW_PRICE = data
        Windows.NWPRICETABLE_WIN = self.nwsetprice
        Windows.NWPRICETABLE_WIN.set_table_data()
        Windows.NWPRICETABLE_WIN.show()
        self.logs_in(ActionStates.RIGHTPASSWORDCUTNWOFFERTABLE)


    def iftext_null(self):
        self.to_message('找不到解析路径!')


    def mousePressEvent(self, event):
        # self.clicked.emit()
        if event.button() == QtCore.Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()  # 获取鼠标相对窗口的位置
            event.accept()
            self.setCursor(QCursor(QtCore.Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, QMouseEvent):
        if QtCore.Qt.LeftButton \
                and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)  # 更改窗口位置
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(QtCore.Qt.ArrowCursor))


    def close_app(self):
        self.logs_in(ActionStates.CLOSEAPPLICATION)
        if Windows.PRICETABLE_WIN is not None:
            if Windows.PRICETABLE_WIN.isVisible() is True:
                Windows.PRICETABLE_WIN.esc_this()
        self.close()
    # 模拟运算子页面
    def get_objid(self):
        a = widgets.lineEdit.text()
        return a

    def get_state(self):
        a = widgets.lineEdit_3.text()
        return a

    def get_kg(self):
        a = widgets.lineEdit_5.text()
        return a

    def simulate_operation_ui(self):
        return so.simulation_operation_run(self.get_objid(),
                                           self.get_state(),
                                           self.get_kg())

    def response_result(self):
        result = self.simulate_operation_ui()
        if result is False:
            self.if_null()
            return
        r0 = str(result[0])
        r1 = str(result[1])
        r2 = str(result[2])
        widgets.textBrowser.setText(r0)
        widgets.textBrowser_2.setText(r2)
        widgets.textBrowser_4.setText(r1)
        widgets.textBrowser_3.setText(r2)

    def rollback_navigation(self):

        self.hide()
        Windows.NAVIGATION_WIN.show()
        Windows.NAVIGATION_WIN.show_anim()
        self.logs_in(ActionStates.ROLLBACKNAVIGATION)



    def if_null(self):
        self.to_message('信息不全!')


    def show_anim(self):
        self.anim0 = QPropertyAnimation(self,
                                        b"windowOpacity")  # 设置动画对象
        self.anim0.setDuration(150)  # 设置动画时长
        self.anim0.setStartValue(0)  # 设置初始属性，1.0为不透明
        self.anim0.setEndValue(1)  # 设置结束属性，0为完全透明
        self.anim0.start()  # 开始动画


    def to_message(self, text):
        Windows.MESSAGE_WIN.set_message(text)


    def dynamic_loading_button(self, arr):
        for self.i in arr:
            self.i = QPushButton(self.tab)
            self.i.setGeometry(QtCore.QRect(70, 30, 151, 121))
            self.i.setStyleSheet("background-color: rgba(211, 255, 255, 0);\n"
                                 "border-top-right-radius:10px;\n"
                                 "border-top-left-radius:10px;\n"
                                 "border-bottom-right-radius:10px;\n"
                                 "border-bottom-left-radius:10px;\n"
                                 "border-image: url(:/images/images/mode1.jpg);")
            self.i.setText("")
            self.i.setObjectName(str(self.i))


    def goto_updatewin(self):
        Windows.UPDATA_WIN = self.update
        Windows.UPDATA_WIN.get_like_names_table_name()
        Windows.UPDATA_WIN.show()
        self.hide()












if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SetOfferWindow()
    window.show()
    sys.exit(app.exec_())